package com.lannuokeji.biz.ac;


import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.ac.entity.User;
import com.lannuokeji.biz.ac.service.UserService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-17
 */
@RestController
@RequestMapping("/ac/userManage")
public class UserManageController extends AController {

	@Resource
	UserService userService;
	@Autowired
	RedisSessionDAO redisSessionDao;

	@ApiOperation(value = "获取在线用户列表")
	@GetMapping("/onlineUserList")
//	@RequiresPermissions("sys.base.session.read")
	public Collection<Session> onlineUserList() {
		return redisSessionDao.getActiveSessions();
	}

	@ApiOperation(value = "删除用户基本信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(name = "id", value = "用户账号", required = true) @PathVariable String id) {
		/*
		 * HttpSession session = request.getSession(); String id =
		 * session.getAttribute("id").toString();
		 */
		if (userService.del(id)) {
			return setSuccessContent("操作成功");
		} else {
			return setErrorContent("操作失败");
		}
	}
	
	@ApiOperation(value = "查看个人基本信息")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "用户id", required = true) @PathVariable String id) {
		User user = userService.selectById(id);
		return setSuccessContent(user);
	}

	@ApiOperation(value = "添加用户")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody User entity) {
		boolean ret = userService.insert(entity);
		if (ret) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}
	@ApiOperation(value = "修改用户信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody User entity) {
		if (userService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
	
	@ApiOperation(value = "修改密码")
	@PostMapping("/updatePassword")
	public Object updatePassword(HttpServletRequest request,
			@ApiParam(name = "password", value = "原密码", required = true) @RequestParam("password") String password,
			@ApiParam(name = "repassword", value = "新密码", required = true) @RequestParam("repassword") String repassword) {
		HttpSession session = request.getSession();
		String id = session.getAttribute("id").toString();
		if (userService.updatePassword(id, password, repassword)) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}

	@ApiOperation(value = "关联查询分页列表")
	@GetMapping("/userList")
	public Object getList(PageRequest pageRequest, User entity) {
		System.out.println("用户id：" + SecurityUtils.getSubject().getPrincipal());
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = userService.page(page, entity);
		return setSuccessContent(page);
	}

	@ApiOperation(value = "审核用户注册信息")
	@GetMapping("/check")
	public Object check(HttpServletRequest request,
			@ApiParam(name = "id", value = "用户账号", required = true) @RequestParam(value = "id", required = true) String id,
			@ApiParam(name = "status", value = "审核状态:1审核通过，3不通过", required = true) @RequestParam(value = "status", required = true) Integer status,
			@ApiParam(name = "back", value = "审核不通过原因", required = false) @RequestParam(value = "back", required = false) String back) {
		/*
		 * HttpSession session = request.getSession(); String id =
		 * session.getAttribute("id").toString();
		 */
		if (userService.check(id, status, back)) {
			return "操作成功";
		} else {
			return "操作失败";
		}
	}

	@ApiOperation(value = "禁止/恢复登录")
	@PostMapping("/invalidLogin")
	public Object invalidLogin(HttpServletRequest request,
			@ApiParam(name = "id", value = "用户账号", required = true) @RequestParam(value = "id", required = true) String id,
			@ApiParam(name = "status", value = "登录状态：1 正常登录 ,2 禁止登录 ", required = true) @RequestParam(value = "status", required = true) Integer status) {
		/*
		 * HttpSession session = request.getSession(); String id =
		 * session.getAttribute("id").toString();
		 */
		if (userService.invalidLogin(id, status)) {
			return setSuccessContent("操作成功");
		} else {
			return setErrorContent("操作失败");
		}
	}

	@ApiOperation(value = "加入黑名单、白名单")
	@PutMapping("/blackList/{id}")
	public Object blackList(HttpServletRequest request,
			@ApiParam(name = "id", value = "用户账号", required = true) @PathVariable String id,
			@ApiParam(name = "blackList", value = "状态:1黑名单、0白名单", required = true) @RequestParam(value = "blackList", required = true) Integer blackList) {
		/*
		 * HttpSession session = request.getSession(); String id =
		 * session.getAttribute("id").toString();
		 */
		if (userService.blackList(id, blackList)) {
			return setSuccessContent("操作成功");
		} else {
			return setErrorContent("操作失败");
		}
	}

	@ApiOperation(value = "修改账户权限")
	@PutMapping("/updateUserAction")
	public Object updateUserAction(HttpServletRequest request,
			@ApiParam(name = "id", value = "用户账号", required = true) @RequestParam(value = "id", required = true) String id,
			@ApiParam(name = "roleId", value = "账号分类id", required = true) @RequestParam(value = "roleId", required = true) Integer roleId) {
		/*
		 * HttpSession session = request.getSession(); String id =
		 * session.getAttribute("id").toString();
		 */
		if (userService.updateUserAction(id, roleId)) {
			return setSuccessContent("操作成功");
		} else {
			return setErrorContent("操作失败");
		}
	}
}

